/**
 * 面试预约列表
 * @author  Jello
 * @create  2018/11/05 09:10
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var workId = "";
var curPage = 1;
var size = 3;
var spinner = new Spinner(opts);

$(function() {
	token = sessionStorage.getItem("user_token") == null ? "" : sessionStorage.getItem("user_token");
	$("#nickname").html(sessionStorage.getItem("nickname"));

	//菜单栏切换
	$(".info").click(function() {
		$(".tab_nav").toggle();
		$("#menubar").attr("src", $(".tab_nav").is(":hidden") ? "img/personal/arrow_bottom.png" : "img/personal/arrow_top.png");
	});

	$(".cover").click(function() {
		$(document).unbind("scroll");
		$(".wran_box").hide();
	})

	$("#box_cancel").click(function() {
		$(document).unbind("scroll");
		$(".intervie_box").hide();
	})

	//查询面试预约列表
	flipOverPage(curPage);

	//确定
	$("#box_yes").click(function() {
		//取消报名接口上送
		var param = {};
		param.workId = workId;
		param.token = token;
		cancelApply(param, function(resultObj) {
			if(resultObj.code == 0 && resultObj.data != null) {
				$(".intervie_box").hide();
				$(".wran_box").show();
				$(".warntext").html("取消成功");
				setTimeout(function() {
					$(".wran_box").hide();
					$(document).unbind("scroll");
					flipOverPage(1);
				}, 800);
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else {
				$(".wran_box").show();
				$(".warntext").val("取消失败");
				return;
			}
		});
	})

	//根据关键字或公司查询
	$("#searchJob").click(function() {
		var companyName = $("#companyKey").val();
		sessionStorage.setItem("keyword", companyName);
		window.location.href = "applyjob.html";
	})
	$("#companyKey").keyup(function(event) {
		if(event.keyCode == 13) {
			var companyName = $("#companyKey").val();
			sessionStorage.setItem("keyword", companyName);
			window.location.href = "applyjob.html";
		}
	});

	//客服中心
	$("#service_btn1").click(function() {
		window.open("tencent://message/?uin=1423335266&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn2").click(function() {
		window.open("tencent://message/?uin=2028048473&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn3").click(function() {
		window.open("tencent://message/?uin=3341118684&Site=lanpin123.com&Menu=yes");
	})
})

/**
 * 查询面试预约
 * @param {Object} curPage
 */
function flipOverPage(curPage) {
	$.ajax({
		type: "get",
		url: serverUrl + "/workapi/get_work_order_api?userToken=" + token + "&page=" + curPage + "&size=" + size + "&status=1",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: 15000,
		dataType: "json",
		crossDomain: true,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			$(".intervie_list").empty();
			if(resultObj.code == 0 && resultObj.data.list.length != 0) {

				for(var i = 0; i < resultObj.data.list.length; i++) {
					var interList = resultObj.data.list
					var temp = interList[i].status; //招聘状态
					var status = "";
					var showtype1 = "";
					var showtype2 = "";
					var color = "#FF6666";
					if(temp == 0) {
						status = "面试预约中";
						showtype1 = "block";
						showtype2 = "none";
						color = "#3CAFFF";
					} else if(temp == 1) {
						status = "面试通过";
						showtype1 = "none";
						showtype2 = "block";
					} else if(temp == 2) {
						status = "面试失败";
						showtype1 = "none";
						showtype2 = "block";
					} else if(temp == 3) {
						status = "已招满";
						showtype1 = "none";
						showtype2 = "block";
					} else if(temp == 4) {
						status = "放弃入职";
						showtype1 = "none";
						showtype2 = "block";
					} else if(temp == 5) {
						status = "成功入职";
						showtype1 = "none";
						showtype2 = "block";
					}

					var interhtml = '<div class="intervie_con"><div class="intervie_info">' +
						'<div class="intervie_txt1">' +
						'<div class="intervie_div1"><div class="title2">应聘岗位 — <span>' + interList[i].workName + '</span></div>' +
						'<div class="title3"><span>' + interList[i].mechanismName + '</span><span class="title5">报名时间</span></div>' +
						'<div class="title4"><img src="img/work/address.png" /><span class="title6" onclick="queryTheLocation(' + interList[i].y + ',' + interList[i].x + ')">去这里</span><span class="title7">' + timestampToTime(interList[i].time) + '</span></div></div>' +
						'<div class="intervie_div2"><div class="title8"></div>' +
						'<div class="title9"><div class="intervieaddr">面试地点：<span class="">' + interList[i].recruitAddress + '</span></div>' +
						'<div>面试时间：<span class="">' + interList[i].interviewTime + '</span></div></div></div></div>' +
						'<div class="intervie_txt2"><div class="status" style="color: ' + color + ';">' + status + '</div>' +
						'<div class="opt">' +
						'<button type="button" style="display: ' + showtype1 + ';" onclick="cancel(' + interList[i].workId + ')">取消报名</button>' +
						'<button type="button" style="display: ' + showtype2 + ';" onclick="deleteApply(' + interList[i].id + ')">刪除</button></div></div>' +
						'</div></div>';

					$(".intervie_list").append(interhtml);

					if(interList[i].teacherList.length != 0) {
						var teacherList = "";
						if(interList[i].teacherList.length > 2) {
							teacherList = interList[i].teacherList.slice(0, 2);
						} else {
							teacherList = interList[i].teacherList;
						}
						for(var j = 0; j < teacherList.length; j++) {
							var teacherList_html = '<div class="homephone1">驻场：<span>' + teacherList[j].teacherName + '-' + teacherList[j].teacherTel + '</span></div>';
							$(".title8").eq(i).append(teacherList_html);
						}
					}
				}

				if(resultObj.data.totalSize > 3) { //显示分页条数
					//分页插件
					$(".intervie_page").show();
					$("#page").paging({
						pageNo: resultObj.data.pageNum,
						totalPage: resultObj.data.totalPages,
						totalSize: resultObj.data.totalSize,
						callback: function(num) {
							curPage = num;
							flipOverPage(curPage);
						}
					})
				} else {
					$(".intervie_page").hide();
				}

			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else { //没有面试预约
				$(".intervie_empty").show();
				$(".intervie_list").hide();
				$(".intervie_page").hide();
			}
		}
	});
}

/**
 * 删除报名
 * @param {Object} id 面试列表id
 */
function deleteApply(id) {
	var id = id;
	$.ajax({
		type: "get",
		url: serverUrl + "/work/del_workorder?userToken=" + token + "&status=1" + "&workOrderId=" + id,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0 && resultObj.data != null) {
				$(".wran_box").show();
				$(".warntext").html("删除成功");
				setTimeout(function() {
					$(".wran_box").hide();
					$(document).unbind("scroll");
					flipOverPage(1);
				}, 800);
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else {
				$(".wran_box").show();
				$(".warntext").val("删除失败");
				return;
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$(".wran_box").show();
			$(".warntext").val("网络异常");
			return;
		}
	});
}

/**
 * 取消报名
 * @param {Object} id 面试预约id
 */
function cancel(id) {
	workId = id;
	$(".intervie_box").show();
	stopScroll();
}

/**
 * 用经纬度设置地图中心点
 */
function queryTheLocation(longitude, latitude) {
	window.open("location.html?longitude=" + longitude + "&latitude=" + latitude);
}